package me.w70.bot.methods;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import me.w70.bot.Bot;
import me.w70.bot.wrappers.RSInterface;

// @author Tim
public class BotGraphics {

	protected BufferedImage full = null;

	protected BufferedImage empty = null;

	protected ImageObserver progress = null;
	
	/*
	 * drawRect(graphics object, rectangle object)
	 * Draw a rectangle on the screen where a rectangle object is for debugging.
	 */
	public Graphics drawRect(Graphics g, Rectangle r) {
		g.setColor(new Color(255,255,255));
		g.drawRect(r.x, r.y, r.width, r.height);
		return g;
	}

	public Graphics getGraphics() {
		return Bot.methods.getClient().getCanvas().getGraphics(); }
	
	/*
	 * interfacedebug(graphics object, interface)
	 * Draws a rectangle around an interface, so for debug purposes.
	 */
	public Graphics interfacedebug(Graphics g, RSInterface i) {
		g.setColor(new Color(255, 255, 255));
		g.drawLine(i.getX(), i.getY(), i.getX() + i.getWidth(), i.getY());
		g.drawLine(i.getX(), i.getY() + i.getHeight(), i.getX() + i.getWidth(), i.getY() + i.getHeight());
		g.drawLine(i.getX(), i.getY(), i.getX() + i.getWidth(), i.getY() + i.getHeight());
		g.drawLine(i.getX() + i.getWidth(), i.getY(), i.getX() + i.getWidth(), i.getY() + i.getHeight());
		return g;
	}
	
	/*
	 * drawarrow(graphics object, x-coordinate, y-coordinate, angle of rotation around center)
	 * Draws an arrow rotated around an invisible circle. X and Y are the
	 * centerpoint of the circle. Angle begins at 0 degrees pointing directly
	 * right, like a unit circle.
	 */
	public Graphics drawarrow(Graphics g, int x, int y, int angle) {
		int modifiedx = ((int) (x + Math.cos(angle)));
		int modifiedy = ((int) (y + Math.sin(angle)));
		g.drawLine(x > modifiedx ? modifiedx : x,
				y > modifiedy ? modifiedy : y, x > modifiedx ? x : modifiedx,
				y > modifiedy ? y : modifiedy);
		return g;
	}

	/*
	 * drawprogressbar(graphics object, x-coordinate, y-coordinate, current status, goal, use
	 * images) Draws a progress bar to the screen. X and Y are the upper-left
	 * corner of the image. imagebased = true for an image-based progress bar,
	 * or false for solid colors.
	 */
	public Graphics drawprogressbar(Graphics g, int x, int y, int width,
			int height, int current, int goal, boolean imagebased) {
		int tofillgreen = (current % width);
		int tofillred = ((goal - current) % width);
		if (imagebased) {
			try {
				g.drawImage(full, x, y, x + tofillgreen, y + full.getHeight(),
						x, y, x + tofillgreen, y + full.getHeight(), progress);
				g.drawImage(empty, x + tofillgreen, y, x + full.getWidth(), y
						+ full.getHeight(), x + tofillgreen, y, x
						+ full.getWidth(), y + full.getHeight(), progress);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			g.setColor(new Color(0, 255, 0));
			g.fillRect(x, y, tofillgreen, height);
			g.setColor(new Color(255, 0, 0));
			g.fillRect(x + tofillgreen, y, tofillred, height);
		}
		return g;
	}

	/*
	 * BotGraphics() Constructor, simply loads external images into their
	 * appropriate objects.
	 */
	public BotGraphics() {
		try {
			full = ImageIO.read(new File(Bot.IMG_DIR + "prog_full.png"));
			empty = ImageIO.read(new File(Bot.IMG_DIR + "prog_empty.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void main() {
		new BotGraphics();
	}
}